<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\validate\energy;

use think\Validate;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;

class EnergyMeterValidate extends Validate
{
    protected $rule = [
        'energy_no|设备编号' => 'require',
        'energy_name|安装地址' => 'require',
        'beilv|计量倍率' => 'require|number',
        'plan_id|收费方案' => 'requireWithout:unit_price|number',
        'unit_price|计量单价' => 'requireWithout:plan_id|float',
        'is_public|安装属性' => 'require|number|in:1,2',
        'off_type|通断方式' => 'require|number|in:1,2',
        'plan_type|缴费类型' => 'require|number|in:1,2,3',
        'energy_type|设备类型' => 'require|number|in:11,12,13,14',
    ];

    public function sceneAdd()
    {
        return $this->remove('id', 'require');
    }

    public function sceneEdit()
    {
        return $this->remove('id', 'require');
    }

    /**
     * 批量添加设备
     * @return void
     *
     * @author Amos
     * 2024-05-18 16:09
     */
    public function sceneBatchAdd()
    {
        $this->only(['beilv', 'meter_data', 'wit_type', 'plan_type', 'property_id'])
            ->remove('beilv', 'require')
            ->append('beilv|倍率', 'number')
            ->append('wit_type|设备类型', 'require|in:1,2')
            ->append('plan_type|缴费类型', 'require|in:1,2,3')
            ->append('property_id|物业信息', 'checkPropertyData')
            ->append('meter_data|设备数据', 'require|checkMeterData');
    }

    /**
     * 扫码添加设备
     * @return void
     *
     * @author Amos
     * 2024-05-18 16:09
     */
    public function sceneCodeAdd()
    {
        $this->only(['energy_name', 'energy_code', 'unit_price', 'plan_type'])
            ->append('energy_code|二维码', 'require')
            ->append('energy_name|安装地址', 'require')
            ->append('plan_type|缴费类型', 'require|in:2,3')
            ->append('unit_price|设备单价', 'require|float');
    }

    /**
     * 设备充值
     * @return EnergyMeterValidate
     *
     * @author Amos
     * 2024-05-09 21:55
     */
    public function sceneRecharge()
    {
        return $this->only(['pay_method', 'pay_amount', 'is_type'])
            ->append('is_type|充值方式', 'require|in:1,2')
            ->append('pay_method|支付方式', 'require|in:1,2,3,4,5')
            ->append('pay_amount|充值金额', 'require|float');
    }


    protected function checkMeterData($value)
    {
        if (!isset($value[0])) {
            return '设备数据错误';
        }
        if (count($value) > 1000) {
            return '一次最多导入1000个表';
        }

        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $addresss = [];
        foreach ($value as $k => $v) {
            $line = $k + 1;
            if (!isset($v['energy_name']) || $v['energy_name'] == '') {
                return '第' . $line . '行数据，安装地址为空请输入';
            }
            if (!isset($v['energy_no']) || $v['energy_no'] == '') {
                return '第' . $line . '行数据，设备编号为空请输入';
            }
            $address = $v['energy_no'];
            if (isset($addresss[$address])) {
                return '第' . $line . '行数据，设备编号和第' . $addresss[$address] . '行设备编号重复请更改';
            }
            $addresss[$address] = $line;
            $exists = $energyMeterRepository->getSearch([])->where('energy_no', $address)->count() > 0;
            if ($exists) {
                return '第' . $line . '行数据，设备编号已存在请更改';
            }
        }
        return true;
    }


    /** 切换收费类型场景
     *
     * @return EnergyMeterValidate
     *
     * @author Amos
     * 2024-05-18 20:11
     */
    public function sceneSwitchPlanType()
    {
        return $this->only(['plan_type', 'balance'])
            ->append('balance|充值金额', 'float')
            ->append('plan_type|收费类型', 'require|in:1,2,3');

    }


    /**
     * 修改设备倍率
     * @return EnergyMeterValidate
     *
     * @author Amos
     * 2024-05-18 22:41
     */
    public function sceneModifyBeilv()
    {
        return $this->only(['beilv', 'remark'])
            ->append('beilv|收费类型', 'require|number')
            ->append('remark|备注说明', 'require');
    }

    /**
     * 设备权益转让
     * @return EnergyMeterValidate
     *
     * @author Amos
     * 2024-05-18 22:56
     */
    public function sceneMakeOver()
    {
        return $this->only(['mobile', 'remark'])
            ->append('mobile|手机号码', 'require|mobile')
            ->append('remark|备注说明', 'require');
    }


    /**
     * 验证物业数据是否存在
     * @param $value
     * @return string|true
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-18 16:10
     */
    protected function checkPropertyData($value)
    {
        /** @var PropertyBasicRepository $propertyBasicRepository */
        $propertyBasicRepository = app()->make(PropertyBasicRepository::class);
        $exists = $propertyBasicRepository->getSearch([])->where('id', $value)->count() > 0;
        if (!$exists) {
            return '物业信息不存在';
        }
        return true;
    }


    /**
     * 收费方式更新
     * @return EnergyMeterValidate
     *
     * @author Amos
     * 2024-06-07 17:10
     */
    public function sceneSwitchPlanPrice()
    {
        return $this->only(['plan_id', 'unit_price'])
            ->append('plan_id', 'number')
            ->append('unit_price', 'float');
    }
}